var express = require('express');
var router = express.Router();
var activityModel = require('../database/model/activityModel')
var {jobModel,cateModel,joinModel} = require('../database/model/jobModel')

/* GET home page. */
router.get('/activity', function(req, res, next) { //查询活动
  activityModel.find().then(result=>{
    res.send({
      code:1,
      msg:'活动列表加载成功',
      data:result
    })
  })
});
router.post('/activity',(req,res)=>{ //新增
  console.log(req.body); //此处req.body的数据字段格式，必须跟表结构一致
  // let obj = {
  //   "poster": "https://tse1-mm.cn.bing.net/th/id/OIP-C.HPgZI7_TxkqIaM_6o5AmbgHaEK?w=322&h=181&c=7&r=0&o=5&dpr=2.25&pid=1.7",
  //   "title": "2019年大学生寒假实习1",
  //   "intro": "囊括各种专业1",
  //   "detail": "新京报快讯 据国家市场监管总局消息，针对媒体报道水饺等猪肉制品检出非洲猪瘟病毒核酸阳性问题，市场监管总局、农业农村部已要求企业"
  // }
  // // 向数据库存放数据
  new activityModel(req.body).save().then(result=>{ 
    // console.log(result);
    res.send({
      code:1,
      msg:'录入成功'
    })
  })
})

router.delete('/activity',(req,res)=>{ //删除
  console.log(req.query);
  let {_id} = req.query
  activityModel.deleteOne({_id}).then(result=>{
    res.send({
      code:1,
      msg:'删除成功'
    })
  })
})

router.post('/job',(req,res)=>{ //批量录入岗位
  // console.log(req.body); 
  let {data} = req.body
  // console.log(data[0]);
  // new jobModel(data[0]).save().then(result=>{ //新增一条
  //   console.log(result);
  // })

  jobModel.insertMany(data).then(result=>{
    res.send({
      code:1,
      msg:'批量转存成功'
    })
  })
})

router.get('/catelog', function(req, res, next) { //查询分类
  cateModel.find().then(result=>{
    res.send({
      code:1,
      msg:'分类列表加载成功',
      data:result
    })
  })
});

router.get('/job', function(req, res, next) { //查询岗位
  let {_id} = req.query
  let obj = {}
  if(_id){
    obj._id = _id
  }
  jobModel.find(obj).then(result=>{
    res.send({
      code:1,
      msg:'岗位列表加载成功',
      data:result
    })
  })
});

router.get('/join', function(req, res, next) { //查询报名
  // let {userId,jobId} = req.query
  joinModel.findOne(req.query).then(result=>{
    res.send({
      code:1,
      msg:'报名状态查询成功',
      data:result
    })
  })
});

router.post('/join', function(req, res, next) { //报名
  // let {userId,jobId} = req.query
  new joinModel(req.body).save().then(result=>{
    res.send({
      code:101,
      msg:'报名成功'
    })
  })
});

router.get('/join/list', function(req, res, next) { //查询报名列表
  // let {userId,jobId} = req.query
  joinModel.find().then(result=>{
    res.send({
      code:1,
      msg:'报名列表加载成功',
      data:result
    })
  })
});

router.put('/join/list', function(req, res, next) { //审核岗位
  // let {userId,jobId} = req.query
  console.log(req.query,req.body);
  joinModel.updateOne(req.query,req.body).then(result=>{
    res.send({
      code:1,
      msg:'报名审核成功'
    })
  })
});

// 方法1：存放的文件，缺少必要的后缀
// const multer  = require('multer')
// const upload = multer({ dest: 'public/uploads/' }) //配置中间件，指定图片存放位置
// router.post('/upload', upload.single('picture'), function (req, res, next) {
//   console.log(req.file);
//   // req.file is the `avatar` file
//   // req.body will hold the text fields, if there were any
// })

// 方法2: 自定义图片名称、路径
const multer  = require('multer')
const storage = multer.diskStorage({ //自主控制上传行为
  destination: function (req, file, cb) { //约定文件存放位置
    cb(null, 'public/uploads/')
  },
  filename: function (req, file, cb) { //约定文件存放名称
    // console.log('multer自定义配置',file);
    let type = file.mimetype.split('/')[1]  //获取图片后缀
    let name = Math.random().toString(36).substring(2)+Date.now() //自定义图片名称，保证唯一性
    cb(null, `${name}.${type}`)
  }
})
const upload = multer({ storage: storage })
router.post('/upload', upload.single('picture'), function (req, res, next) {
  console.log(req.file);
  res.send({
    code:1,
    msg:'上传成功',
    data:`/uploads/${req.file.filename}`
  })
  // req.file is the `avatar` file
  // req.body will hold the text fields, if there were any
})


module.exports = router;
